package com.funtonow.jqdk.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.funtonow.jqdk.R;
import com.funtonow.jqdk.adapter.LoanListAdapter;
import com.funtonow.jqdk.base.BaseActivity;
import com.funtonow.jqdk.base.Constants;
import com.funtonow.jqdk.base.SqjAppliaction;
import com.funtonow.jqdk.bean.GameThirdInfoListBean;
import com.funtonow.jqdk.bean.LoanInfoBean;
import com.funtonow.jqdk.utils.SPUtil;
import com.funtonow.jqdk.utils.StatisticsUtils;
import com.funtonow.jqdk.utils.ToastUtils;
import com.funtonow.jqdk.widget.NoScrollWebView;
import com.github.ybq.android.spinkit.SpinKitView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author Jiajien
 * @date 2019/8/2
 */
public class ZxWebViewActivity extends BaseActivity {

    @BindView(R.id.wv_view)
    NoScrollWebView mWebView;
    @BindView(R.id.spin_kit)
    SpinKitView spinKitView;
    @BindView(R.id.rlv_zx)
    RecyclerView recyclerView;
    @BindView(R.id.ll_zx_recommend)
    LinearLayout llZxRecommend;

    private String title;
    private LoanListAdapter loanListAdapter;
    private LoanInfoBean loanInfoBean;
    private List<GameThirdInfoListBean> gameThirdInfoListBeans = new ArrayList<>();

    @Override
    public int getLayoutRes() {
        return R.layout.activity_zx_webview;
    }

    @Override
    public void onCreateActivity(@Nullable Bundle savedInstanceState) {
        title = getIntent().getStringExtra("title");
        initWeb();
        initRecyclerView(recyclerView);
        loanInfoBean = SqjAppliaction.loanInfoBean;
        loanListAdapter = new LoanListAdapter();
        recyclerView.setAdapter(loanListAdapter);
        setData();
        loanListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (SPUtil.getUserInfo() == null) {
                    Intent intent = new Intent(ZxWebViewActivity.this, LoginActivity.class);
                    startActivity(intent);
                } else if (SPUtil.getUserInfo() != null && loanListAdapter.getItem(position).getVipfw() == 1
                        && SPUtil.getUserInfo().getHasVip() == 0) {
                    ToastUtils.show("该产品仅限会员申请");
                    startActivity(new Intent(ZxWebViewActivity.this, VipActivity.class));
                } else {
                    StatisticsUtils.clickUrl(loanListAdapter.getItem(position));
                    StatisticsUtils.intentWebView(ZxWebViewActivity.this,
                            loanListAdapter.getItem(position).getUrl(),loanListAdapter.getItem(position).getName(),
                            loanListAdapter.getItem(position).getChanpinid());
                }
            }
        });
        StatisticsUtils.baoGuang(Constants.HEZI_ID_26);
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initWeb() {
        initToolbar(title);
        ibNews.setVisibility(View.GONE);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setDomStorageEnabled(true);
        mWebView.setWebChromeClient(new WebChromeClient());
        mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                spinKitView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                spinKitView.setVisibility(View.GONE);
                llZxRecommend.setVisibility(View.VISIBLE);
            }


        });

        mWebView.loadUrl(getIntent().getStringExtra("url"));
        //mWebView.loadUrl("https://mp.weixin.qq.com/s/jaY_UTBE6DRrWYV9dLM7XQ");
    }

    private void initRecyclerView(RecyclerView recyclerView) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        //设置增加或删除条目的动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }


    public void setData() {
        if (gameThirdInfoListBeans.size() > 0) {
            gameThirdInfoListBeans.clear();
        }
        for (LoanInfoBean.HeziInfoListBean heziInfoListBean : loanInfoBean.getHeziInfoList()) {
            if (heziInfoListBean.getType() == 26) {
                JSONObject jsonObject = JSON.parseObject(loanInfoBean.getCpMap());
                for (LoanInfoBean.GdlistBean gdlistBean : loanInfoBean.getGdlist()) {
                    if (heziInfoListBean.getGdid().equals(gdlistBean.getGdid())) {
                        GameThirdInfoListBean gameThirdInfoListBean = JSON.parseObject(jsonObject.getJSONObject(gdlistBean.getChanpinid()).toJSONString(), GameThirdInfoListBean.class);
                        gameThirdInfoListBean.setVipfw(gdlistBean.getVipfw());
                        gameThirdInfoListBean.setUrl(gdlistBean.getUrl());
                        gameThirdInfoListBean.setHeziId(heziInfoListBean.getType());
                        gameThirdInfoListBean.setIndex(heziInfoListBean.getInde());
                        gameThirdInfoListBean.setGdId(gdlistBean.getGdid());
                        gameThirdInfoListBeans.add(gameThirdInfoListBean);
                    }
                }
                loanListAdapter.setNewData(gameThirdInfoListBeans);
            }
        }
    }

}
